package com.xiaoma.controller;


import com.xiaoma.pojo.Result;
import com.xiaoma.pojo.Software;
import com.xiaoma.service.SoftwareListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api")
public class SoftwareListController{
    @Autowired
    private SoftwareListService softwareListService;

    /**
     *添加软件
     */
    @PostMapping("addSoftware")
    public Result<Software> addSoftware(@RequestBody Software software){
        softwareListService.addSoftware(software);
        //log.info("添加软件成功:{}", software);
        return Result.success();
    }


    /**
     * 删除软件
     * @param id
     * @return
     */
    @DeleteMapping("deleteSoftware")
    public Result<Software> deleteSoftware(@RequestParam("id") Integer id){
        softwareListService.deleteSoftware(id);
        //log.info("删除软件成功:{}", id);
        return Result.success();
    }

    /**
     * 修改软件
     */
    @PutMapping("updateSoftware")
    public Result<Software> updateSoftware (@RequestBody Software software){
        softwareListService.updateSoftware(software);
        //log.info("修改软件成功:{}", software);
        return Result.success();
    }

    /**
     * 查询软件列表
     * @return
     */
    @GetMapping("softwareList")
    public Result<List<Software>> getSoftwareList() {
        List<Software> softwareList = softwareListService.getSoftwareList();
        //log.info("查询结果:{}", softwareList);
        return Result.success(softwareList);
    }

    /**
     * 根据id进行软件信息的回显
     */
    @GetMapping("getSoftwareById/{id}")
    public Result<Software> getSoftwareById(@PathVariable("id") Integer id) {
        Software software = softwareListService.getSoftwareById(id);
        //log.info("查询结果:{}", software);
        return Result.success(software);
    }

}
